﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using ChoViet.Lib.Common;
using ChoViet.Model;
using System.Web.Mvc;

namespace ChoViet.Model
{
    public class SiteUserManager
    {
        public static User GetMember(string userName)
        {
            try
            {
                ChoVietDataContext dt = new ChoVietDataContext();
                var us = dt.Users.FirstOrDefault(e => e.UserName == userName);
                return us;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        public static User GetUser(object userNameorKey, bool iskey)
        {
            try
            {
                ChoVietDataContext edt = new ChoVietDataContext();
                var us = iskey ? edt.Users.FirstOrDefault(e => e.ID == Convert.ToInt32(userNameorKey) && e.OperationYN == true) : edt.Users.FirstOrDefault(e => e.UserName == userNameorKey && e.OperationYN == true);                 
                
                if (us == null) return null;
                return us;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static int GetUserKey(string userName)
        {
            try
            {
                ChoVietDataContext edt = new ChoVietDataContext();
                var id = edt.Users.FirstOrDefault(e => e.UserName == userName).ID;
                return id > 0 ? id : 0;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        
        public static bool CheckByEmail(string email)
        {
            try
            {
                ChoVietDataContext edt = new ChoVietDataContext();
                var isexit = edt.Users.FirstOrDefault(e => e.Email == email).ID;
                return (isexit != null && isexit >0) ? false : true;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static bool CheckUserName(string userName)
        {
            try
            {
                ChoVietDataContext edt = new ChoVietDataContext();
                var isexit = edt.Users.FirstOrDefault(e => e.UserName == userName).ID;
                return (isexit != null && isexit > 0) ? false : true;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static User GetByEmail(string email) 
        {
            try
            {
                ChoVietDataContext edt = new ChoVietDataContext();
                var us = edt.Users.FirstOrDefault(e => e.Email == email);
                return us;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
    }
}
